#include "../../include/StudentController.h"

/**
 * 有参构造
 */
StudentController::StudentController() {}
/**
 * 析构函数
 */
StudentController::~StudentController() {
    clearMap(studentMap);
}
/**
 * 有参创建学生 
 * @param string name 学生姓名
 */
Student* StudentController::createStudent(std::string name) {
    IdManager* idManager = IdManager::getIdManager();
    std::string stuId = idManager->createId("STU");
    Student* newStu = new Student(stuId,name);
    studentMap[stuId] = newStu;
    return newStu;
}
/**
 * 有参创建学生 
 * @param string id 学生学号
 * @param string name 学生姓名
 */
Student* StudentController::createStudent(std::string id,std::string name) {
    IdManager* idManager = IdManager::getIdManager();
    idManager->addId(name,id);
    Student* newStu = new Student(id,name);
    studentMap[id] = newStu;
    return newStu;
}
/**
 * 获取学生数量
 */
int StudentController::getStudentCount() {
    return studentMap.size();
}
/**
 * 获取学生-通过ID
 */
Student* StudentController::findStudentById(std::string id) {
    if(studentMap.find(id) == studentMap.end()) {
        return nullptr;
    }
    return studentMap[id];
}

void StudentController::listAllStudent(Student* (&studentList)[]) {
    std::unordered_map<std::string,Student*>::iterator iter;
    int idx = 0;
    for(iter=studentMap.begin();iter!=studentMap.end();iter++) {
        studentList[idx] = iter->second;
        idx += 1;
    }
}
/**
 * 删除学生 
 * @param string name 学生姓名
 */
void StudentController::deleteStudent(Student* stu) {
    studentMap.erase(stu->id);
    // idCreater->countDecrease("STU");
    delete stu;
}
/**
 * 从字符串中解析出学生类
 */
Student* StudentController::strToStudent(std::istringstream &iss) {
    char split = ' ';
	std::string id;
    std::getline(iss, id, split);
    std::string name;
    std::getline(iss, name, split);
    Student* student = this->createStudent(id,name);
    return student;
}

